package com.example.plant.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.plant.R
import com.example.plant.entity.RecognizedInfo

import androidx.lifecycle.ViewModelProvider

import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider.NewInstanceFactory
import com.example.plant.databinding.ActivityMainBinding
import android.content.IntentFilter

import com.example.plant.broadcast.GetPlantClassifyResultBroadCast
import androidx.activity.result.ActivityResultLauncher
import android.graphics.Bitmap
import android.graphics.ImageDecoder
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import com.example.plant.util.http.httpUtil
import com.example.plant.util.images.RegisterActivityForResultUtil
import com.jaeger.library.StatusBarUtil


class MainActivity : AppCompatActivity() {

    object list {
        var TAG: String = "MainActivity"
    }

    var recognizedInfo1: RecognizedInfo = RecognizedInfo()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val mainBinding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)
        mainBinding.lifecycleOwner = this;
        recognizedInfo1 =
            ViewModelProvider(this, NewInstanceFactory()).get(RecognizedInfo::class.java)
        mainBinding.recognizedInfo = recognizedInfo1
        httpUtil.send()
        init()
        StatusBarUtil.setColor(this,getResources().getColor(R.color.white),0)
        registerBroadCast()
    }

    val HAS_PLANT_CLASSIFY_RESULT = "getPlantClassifyResult"


    /**
     * ai 植物识别 结果的广播
     */
    private var getPlantClassifyResultBroadCast: GetPlantClassifyResultBroadCast? = null
    private fun registerBroadCast() {
        getPlantClassifyResultBroadCast = GetPlantClassifyResultBroadCast(recognizedInfo1)
        registerReceiver(getPlantClassifyResultBroadCast, IntentFilter(HAS_PLANT_CLASSIFY_RESULT))
    }


    /**
     * 注销广播
     */
    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(getPlantClassifyResultBroadCast)
    }

    /**
     * 初始化相机调用
     */
    private var cameraCaptureLauncher: ActivityResultLauncher<Bundle>? = null
    fun init() {
        // 初始化相机调用
        cameraCaptureLauncher =
            RegisterActivityForResultUtil.registerForCameraCaptureResult(this) { bitmapBundle ->
                Log.e(MainActivity.list.TAG, "init: " + "相机调用返回了")
                recognizedInfo1.image.set(bitmapBundle.bitmap)
                handleImage(bitmapBundle.bitmap)
            }
    }


    /**
     * 初始化相册调用
     */
    val pickLauncher =
        registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            uri?.let { it ->
                Log.d("tag", it.toString())
                val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    ImageDecoder.decodeBitmap(
                        ImageDecoder.createSource(
                            this.contentResolver,
                            it
                        )
                    )
                } else {
                    MediaStore.Images.Media.getBitmap(this.contentResolver, it)
                }
                handleImage(bitmap)
            }
        }

    /**
     * 按钮 选择照片
     */
    fun pictureSelect(view: View) {
        pickLauncher.launch("image/*")
    }


    /**
     * 按钮 拍照
     */
    fun cameraCapture(view: View) {
        cameraCaptureLauncher?.launch(null)
    }

    override fun sendBroadcast(intent: Intent?) {
        super.sendBroadcast(intent)
    }

    /**
     * 对回调的 bitmap 进行处理
     */
    fun handleImage(bitmap: Bitmap?) {
        recognizedInfo1.image.set(bitmap)
        bitmap?.let { httpUtil.getClassify(it,this) }
    }



}
